<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 公司集团数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantGroupService;
use think\Request;

class TenantGroupController extends VerifyController
{
    /**
     * @var TenantGroupService
     */
    protected $tenantGroupService;

    public function __construct()
    {
        parent::__construct();
        $this->tenantGroupService = new TenantGroupService();
    }

    /**
     * 获取集团列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function select(Request $request)
    {
        $param = $request->param();
        $resData = $this->tenantGroupService->select($param['param'], $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 当前租户获取集团列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function selectCurrentTenantGroup(Request $request)
    {
        $resData = $this->tenantGroupService->selectCurrentTenantGroup($request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 新增集团
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function create(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupService->create($param['data'], $request->getTenantId(), $request->getUserId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 转让集团
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function transfer(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupService->transfer($param['data'], $request->getTenantId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 解散集团
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function dissolve(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupService->dissolve($param['data'], $request->getTenantId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     *  更新集团
     */
    public function update(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupService->update($param['data'], $request->getTenantId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

}